<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/plugins/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/plugins/jquery/jquery.min.js"></script>
    <script src="/js/plugins/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
    <link rel="stylesheet" href="/js/plugins/chosen/component-chosen.css"/>
    <script src="/js/plugins/chosen/chosen.jquery.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.js"></script>
    <script src="/js/common.js"></script>


    <script>
        $(function () {

            //获取所有的地区
            $.get('/regions', function (data) {
                var html = "<option></option>";

                //循环取出data中的数据
                $.each(data, function (index, ele) {
                    html += "<option value='" + ele.id + "'>" + ele.name + "</option>"
                    //地区的id
                    //console.log(ele.id);
                    //地区的名字
                    //console.log(ele.name);
                })
                $("#place").html(html)

                //初始化组件
                $("#place").chosen({
                    placeholder_text_single: "请选择地点",
                    disable_serch_threshold: 5,//大于10条就显示搜索框
                    no_results_text: "无相关地区!" // 搜索时找不到的提示
                })
            })

            //获取当前登录用户的信息
            var user = JSON.parse(sessionStorage.getItem("user"))

            //表单提交地址

            var url = "/users/" + user.id + "/travels"

            //获取url上的id参数值,如果有值就是编辑,发请求获取该游记相关信息
            var params = getParams();

            if (params.id) {

                url = "/travels/" + params.id;//(编辑表单提交地址)

                //编辑操作    修改表单的提交方式为put
                $("[name=_method]").val('put')

                //发请求,根据id获取游记对象
                $.get('/travels/' + params.id, function (data) {
                    $(".travels").renderValues(data)

                    //回显地区下拉框
                    //先用普通方式回显
                    $("#place option[value=" + data.place.id + "]").prop('selected', true);

                    //再让插件执行更新操作
                    $("#place").trigger("chosen:updated")
                })
            }

            //修改封面按钮
            $("#changeBtn").click(function () {
                //找到文件框,触发点击事件
                $("#imgInput").click()
            })

            //change事件
            $("#imgInput").change(function () {
                //如果有值就提交
                if ($(this).val()) {
                    $("#imgForm").ajaxSubmit(function (data) {
                        if (data.status == 1) {
                            //把新图片的url回显到img
                            $("#coverUrl").attr('src', data.url)
                        }
                    })
                }
            })

            //设置表单的地址
            //$("#detailForm").attr("action","/users/"+user.id+"/travels")

            //针对表单的属性配置
            //beforeSubmit也可以用于表单的数据验证(如果输入数据不合法,不允许提交)
            //在提交表单之前添加额外的参数
            var options = {
                url: url,   //表单提交的地址
                beforeSubmit: function (arr) {  //提交表单之前,触发的函数

                    //arr是我们要提交的参数数组
                    //提交封面,   name是名字,value是属性值
                    arr.push({name: "coverUrl", value: $("#coverUrl").attr("src")})

                    //提交标题
                    arr.push({name: "title", value: $("#title").val()})

                    //提交获取的游记内容(使用编辑器的方法获取问斩该内容)
                    //elEditor  编辑器,    属性值用编辑器的方法获取
                    arr.push({name: "travelContent.content", value: elEditor.getContent()})
                    arr.push({name: "state", value: state}) //提交状态
                },
                success: function (data) {   //回调函数
                    if (data.success) {
                        //成功后,跳转到我的游记页面
                        window.location.href = "/mine/travels.html"
                    } else {
                        //失败直接输出错误信息
                        alert(data.msg)
                    }
                }
            }


            //把表单该为异步提交
            //先声明回调函数要做什么,不提交,等普通提交时,再用传统的submit方式执行提交
            $("#detailForm").ajaxForm(options)

            var state = 0  //默认草稿状态

            //保存按钮
            $("#save").click(function () {

                $("#detailForm").submit()
            })

            //发布按钮
            $("#release").click(function () {

                //待发布状态
                state = 1
                $("#detailForm").submit()

            })

        })

    </script>


</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="save"><span>保存</span></i>
            <i class="fa fa-check" id="release"><span>发布</span></i>
        </div>
    </div>
</div>

<form action="/images" id="imgForm" method="POST" enctype="multipart/form-data">

    <input type="file" name="file" style="display: none" id="imgInput">
</form>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl" jsagfvis vwyyguesvtgruewfuewaoi/>
        <!--<input type="file" name="pic" style="display: none" />-->
        <button type="button" class="btn" id="changeBtn">
            <i class="fa fa-pencil-square-o"></i>
            <small>更换封面</small>
        </button>

    </div>
    <div class="title">
        <input class="form-control" id="title" placeholder="请输入标题" render-value="title"/>
    </div>
    <h6 href="#">点击文章内容即可编辑</h6>
    <div id="contentEditor" render-html="travelContent.content">
    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" method="post">
            <input type="hidden" name="_method"/>
            <input type="hidden" name="id" render-value="id"/>
            <label>出行时间:</label>
            <input type="text" name="travelTime" class="form-control" onclick="WdatePicker()" render-value="travelTime">
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control" render-value="days">
            <label>参与人物:</label>
            <select class="c-select form-control" name="person" render-value="person">
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" class="form-control" render-value="perExpends">

            <label>旅游地点:</label>
            <select class="form-control" id="place" name="place.id">

            </select>

            <div class="form-check">
                <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" name="isPublic"
                           render-attr="checked=isPublic"
                    >是否公开
                </label>
            </div>

        </form>
    </div>

    <hr/>

</div>
<!--Eleditor核心文件-->
<!--打开页面才用到,可以放最后,渲染效果好-->
<script src="/js/plugins/eleditor/Eleditor.js"></script>
<!--图片上传-->
<script src="/js/plugins/eleditor/webuploader.min.js"></script>

<script>
    var elEditor = new Eleditor({
        el: '#contentEditor',
        upload: {
            //图片上传地址
            server: '/images'
        }
    });
</script>

</body>

</html>